// pages/goods_detail/index.js
import {
  showToast,
  request
} from "../../utlis/asyncWx"
// import regeneratorRuntime from "../../lib/runtime/runtime";
Page({
  data: {
    goodsObj: {},
    collectList: [],
    isCollect: false,
    cartCount: 0,
    isShow: false
  },
  thisGoodsInfo: {},

  // 轮播图预览大图
  handlePrevewImage(e) {
    // 构建数组,map不改变元数据
    const urls = this.data.goodsObj.pics.map(v => v.pics_mid)
    const current = e.currentTarget.dataset.url
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    });
  },
  // 加入购物车
  handleCartAdd() {
    console.log('点击了加入购物车');
    //先还原动画
    this.animations.translateY(-150).opacity(1).step();
    this.setData({
      animation: this.animations.export(),
      isShow: true
    })
    //再执行动画
    this.animation.translateY(0).opacity(1).step();
    this.setData({
      animation: this.animation.export()
    })
    // 1 读取缓存 --数组
    let cart = wx.getStorageSync('cart') || [];
    // 2 查找索引,收否有该id商品
    let index = cart.findIndex(v => v.goods_id === this.thisGoodsInfo.goods_id)
    if (index === -1) { // 如果不存在索引
      this.thisGoodsInfo.num = 1 // 则创建num属性
      this.thisGoodsInfo.check = true // 则创建选中状态
      cart.push(this.thisGoodsInfo)
    } else { // 存在
      cart[index].num++
    }
    this.setData({
      cartCount: cart.length
    })
    wx.setStorageSync('cart', cart) // 写入缓存
    // 消息提醒
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      mask: 'true'
    });
  },
  // 点击收藏
  async onTapCollect() {
    console.log('点击了收藏');
    let isCollect = false
    let collectList = wx.getStorageSync('collectList') || []
    console.log(collectList);
    // 获取当前收藏商品id
    let index = collectList.findIndex(v => v.goods_id === this.thisGoodsInfo.goods_id)
    // 如果index为-1,表示没有对应id
    if (index !== -1) { // 有就删除--取消收藏
      collectList.splice(index, 1)
      isCollect = false
      wx.showToast({
        title: '取消成功',
        icon: 'success',
        mask: true,
        success: (result) => {},
        fail: () => {},
      });
    } else { // 没有就增加--收藏
      collectList.push(this.thisGoodsInfo)
      isCollect = true
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        mask: true,
        success: (result) => {},
        fail: () => {},
      });
    }
    // 写入缓存
    wx.setStorageSync("collectList", collectList)
    this.setData({
      isCollect
    })
  },
  // 请求当前id商品详情
  async getGoodsDetail(goods_id) {
    const {
      data: res
    } = await request({
      url: "goods/detail",
      data: {
        goods_id
      }
    })
    const goodsObj = res.message
    this.thisGoodsInfo = res.message
    console.log(this.thisGoodsInfo)
    this.setData({
      // 设置轮播图数据,价格,名字,详情,商品id
      goodsObj: {
        pics: goodsObj.pics,
        goods_price: goodsObj.goods_price,
        goods_name: goodsObj.goods_name,
        goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, '.jpg'),
        goods_id: goodsObj.goods_id
      }
    })
    let cart = wx.getStorageSync('cart') || [];
    // 获取收藏列表
    let collectList = wx.getStorageSync('collectList') || []
    // .some数组中有一个满足则返回true
    let isCollect = collectList.some(v => v.goods_id === this.data.goodsObj.goods_id)
    this.setData({
      cartCount: cart.length,
      isCollect
    })
  },
  onLoad(options) {
    console.log("onLoad")
    const {
      goods_id
    } = options
    this.getGoodsDetail(goods_id)
  },
  onShow() {
    console.log('onshow');
  },
  onReady: function () {
    //第一个动画，控制图标飘入购物车
    this.animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'linear',
      transformOrigin: 'left top 0',
      success: function (res) {}
    })
    //第二个动画，飘入购物车后，图标还原到原来的位置
    this.animations = wx.createAnimation({
      duration: 0,
      timingFunction: 'linear',
      transformOrigin: 'left top 0',
      success: function (res) {}
    })
  }
})